# @Time:2021/6/7 20:36
# @Author:5120195554王欣霏
# -*- coding = utf-8 -*-

import os
import sys
import pygame
from scene import SceneResult
from scene.the_end_scene import TheEndScene
from actor.swk import SWK
from scene.village_scene import VillageScene
from scene.temple_scene import TempleScene
from scene.start_scene import StartScene
from scene.mountain_scene import MountainScene


def main():
    pygame.init()
    # pygame.mixer.init()  # 混响器
    # 以下部分都被封装到village_scene中了
    # backtmxpath = os.path.join('../resource','tmx','village1.tmx')
    # screen = pygame.display.set_mode((800,600),pygame.RESIZABLE) #主图层
    # pygame.display.set_caption("西游记")
    #
    # # 这里可以使用TileScene类来代替
    # # tr = tiled.TiledRenderer(backtmxpath)#通过tiledrender的构造器构造出了tiledrender的对象
    # # backsurface = pygame.Surface(tr.pixel_size)#背景图层
    # # tr.render_map(backsurface)#渲染图层
    # # winsurface = backsurface.subsurface((0,0,800,600))#0,0表示没有偏移量
    #
    # #加载.tmx图片
    # ts = tilescene(backtmxpath)
    # backsurface = ts.surface
    #
    # #创建障碍物的精灵组
    # obstacle_group = pygame.sprite.Group()
    #
    #
    # dx_dy = [0,0] #这个值等于孙悟空移动的值
    # #孙悟空
    # swk_player = SWK()
    #
    # #孙悟空的位置加载进来之后 可以算出孙悟空相对于整个图片的位置 然后调用函数计算出孙悟空相对于窗体的位置
    # #取出瓦格图片有关的数据
    # for group in ts.tiled.tmx_data.objectgroups:
    #     if isinstance(group,pytmx.pytmx.TiledObjectGroup): #判断是不是瓦格的对象组
    #         if group.name == 'god':
    #             for obj in group:
    #                 earth_god = EarthGod(obj.x,obj.y) #传入相对于.tmx图片的位置
    #         if group.name == 'actor': #判断是不是actor对象组
    #             for obj in group: #遍历actor对象组
    #                 if obj.name == 'sun': #判断是否有孙悟空
    #                     swk_player.set_pos(obj.x,obj.y) #将孙悟空全局的位置赋值给pos_x和pos_y
    #                     win_pos_x = obj.x - 400 #将孙悟空显示在中间   窗口的大小是(800,600)
    #                     win_pos_y = obj.y - 300
    #         if group.name == 'obstacle':#判断是不是障碍物的对象组
    #             for obj in group:
    #                 obs = pygame.sprite.Sprite()#为每一个障碍物都创建一个精灵对象
    #                 obs.rect = pygame.Rect(obj.x,obj.y,obj.width,obj.height)
    #                 obstacle_group.add(obs)
    #
    # winsurface = backsurface.subsurface((win_pos_x,win_pos_y,800,600))#第一二个参数是winsurface的起始位置
    #
    # tempsurface = pygame.Surface((800,600)) #临时窗体 为了解决重影问题 大小和窗体一样
    # clock = pygame.time.Clock() #解决时钟问题
    # #主图层
    # while True:
    #     key_down = False
    #     dx_dy = [0,0]
    #     for event in pygame.event.get():
    #         if event.type == QUIT:
    #             exit()
    #         if event.type ==KEYDOWN:
    #             key_down = True
    #             pressed_key = event.key
    #             dx_dy = swk_player.key_move(pressed_key,key_down,obstacle_group)
    #             if key_down == True:
    #                 win_pos_x += dx_dy[0]
    #                 win_pos_y += dx_dy[1]
    #     #越界判断
    #     if win_pos_x < 0:
    #         win_pos_x = 0
    #     if win_pos_y < 0:
    #         win_pos_y = 0
    #     if win_pos_x > (3780 - 800):
    #         win_pos_x = 3780 - 800
    #     if win_pos_y > (2395 - 600):
    #         win_pos_y = 2395 - 600
    #     # earth_god.Move(1,1)
    #     earth_god.Move()
    #     winsurface = backsurface.subsurface((win_pos_x,win_pos_y,800,600))#每次都要获取一次winsurface
    #     tempsurface.blit(winsurface,(0,0)) #每次都会覆盖前面的内容
    #     swk_player.draw(tempsurface,win_pos_x,win_pos_y) #画孙悟空 孙悟空是画在tempsurface上的 不影响winsurface
    #     earth_god.draw(tempsurface,win_pos_x,win_pos_y)
    #     # screen.blit(winsurface,(0,0)) #将winsurface绘制在屏幕上
    #     screen.blit(tempsurface,(0,0)) #将tempsurface绘制在屏幕上
    #     # swk_player.Move(3,3) #设置为3个像素 像素为正时向正方向走 像素为负时向负方向走
    #     pygame.display.update()
    #     clock.tick(15) #设置每次循环的帧率
    screen = pygame.display.set_mode((800, 600), pygame.RESIZABLE)
    pygame.display.set_caption("西游记")
    icon = pygame.image.load("../resource/img/title.png")  # 载入图片
    pygame.display.set_icon(icon)
    swk = SWK()
    screen_list = ['StartScene', 'VillageScene', 'TempleScene','MountainScene']
    for item in screen_list:
        item_scene = globals()[item](swk, screen)  # 用[]中的值替换()中的值
        item_scene.run()
        print(item_scene.scene_result)
        if item_scene.scene_result == SceneResult.Fail:
            final_result = SceneResult.Fail
            break
        if item_scene.scene_result == SceneResult.Win:
            final_result = SceneResult.Win
            break
        if item_scene.scene_result == SceneResult.Quit:
            final_result = SceneResult.Fail
            break

    # village_scene = VillageScene(swk,screen)
    # village_scene.run()
    # if village_scene.scene_result == SceneResult.Next:
    #     temple_scene = TempleScene(village_scene.swk_player,screen)
    #     temple_scene.run()
    # the_end_scene = TheEndScene(final_result,swk,screen)
    the_end_scene = TheEndScene(final_result)
    the_end_scene.run(screen)
    pygame.quit()  # 退出
    sys.exit()


if __name__ == '__main__':
    main()
